package com.ruoyi.web.schedule;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.web.constant.RedisMessageType;
import com.ruoyi.web.domain.MiningConfig;
import com.ruoyi.web.mapper.MiningConfigMapper;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

/**
 * 挖矿记录造数据
 */
@Component
@EnableScheduling
@AllArgsConstructor
public class MiningConfigTask {

    private final MiningConfigMapper configMapper;

    public final RedisCache redis;

    @Scheduled(fixedDelay = 2000)
    public void task() throws ParseException {
        List<MiningConfig> configs = configMapper.selectMiningConfigList(new MiningConfig());
        if (configs.size() == 0) {
            return;
        }
        MiningConfig config = configs.get(0);
        MiningConfig redisConfig = redis.getCacheObject(RedisMessageType.MINING_CONFIG);
        if (redisConfig == null) {
            return;
        }
        // 随机数 使用概率进行数据随机变化
        double random = Math.random() * 100;
        int probability = 50;
        if (random > probability) {
            if ((config.getBscNum().subtract(redisConfig.getBscNum())).compareTo(new BigDecimal(500)) > 0) {
                config.setBscNum(config.getBscNum().subtract(BigDecimal.valueOf(Math.random() * 50)));
            } else {
                config.setBscNum(config.getBscNum().add(BigDecimal.valueOf(Math.random() * 50)));
            }
            if ((config.getEthNum().subtract(redisConfig.getEthNum())).compareTo(new BigDecimal(500)) > 0) {
                config.setEthNum(config.getEthNum().subtract(BigDecimal.valueOf(Math.random() * 50)));
            } else {
                config.setEthNum(config.getEthNum().add(BigDecimal.valueOf(Math.random() * 50)));
            }
            if ((config.getTotalOutput().subtract(redisConfig.getTotalOutput())).compareTo(new BigDecimal(1000)) > 0) {
                config.setTotalOutput(config.getTotalOutput().subtract(BigDecimal.valueOf(Math.random() * 100)));
            } else {
                config.setTotalOutput(config.getTotalOutput().add(BigDecimal.valueOf(Math.random() * 100)));
            }
            if ((config.getNotReward().subtract(redisConfig.getNotReward())).compareTo(new BigDecimal(300)) > 0) {
                config.setNotReward(config.getNotReward().subtract(BigDecimal.valueOf(Math.random() * 20)));
            } else {
                config.setNotReward(config.getNotReward().add(BigDecimal.valueOf(Math.random() * 20)));
            }
            if ((config.getWaitBuyBack().subtract(redisConfig.getWaitBuyBack())).compareTo(new BigDecimal(300)) > 0) {
                config.setWaitBuyBack(config.getWaitBuyBack().subtract(BigDecimal.valueOf(Math.random() * 20)));
            } else {
                config.setWaitBuyBack(config.getWaitBuyBack().add(BigDecimal.valueOf(Math.random() * 20)));
            }
            if ((config.getBuyBackDestruction().subtract(redisConfig.getBuyBackDestruction())).compareTo(new BigDecimal(300)) > 0) {
                config.setBuyBackDestruction(config.getBuyBackDestruction().subtract(BigDecimal.valueOf(Math.random() * 20)));
            } else {
                config.setBuyBackDestruction(config.getBuyBackDestruction().add(BigDecimal.valueOf(Math.random() * 20)));
            }
            if (config.getValidNode() - redisConfig.getValidNode() > 50) {
                config.setValidNode((long) (config.getValidNode() - (Math.random() * 3)));
            } else {
                config.setValidNode((long) (config.getValidNode() + (Math.random() * 3)));
            }
            if (config.getPartakeCount() - redisConfig.getPartakeCount() > 50) {
                config.setPartakeCount((long) (config.getPartakeCount() - (Math.random() * 3)));
            } else {
                config.setPartakeCount((long) (config.getPartakeCount() + (Math.random() * 3)));
            }
        } else {
            if ((redisConfig.getBscNum().subtract(config.getBscNum())).compareTo(new BigDecimal(500)) > 0) {
                config.setBscNum(config.getBscNum().add(BigDecimal.valueOf(Math.random() * 50)));
            } else {
                config.setBscNum(config.getBscNum().subtract(BigDecimal.valueOf(Math.random() * 50)));
            }
            if ((redisConfig.getEthNum().subtract(config.getEthNum())).compareTo(new BigDecimal(500)) > 0) {
                config.setEthNum(config.getEthNum().add(BigDecimal.valueOf(Math.random() * 50)));
            } else {
                config.setEthNum(config.getEthNum().subtract(BigDecimal.valueOf(Math.random() * 50)));
            }
            if ((redisConfig.getTotalOutput().subtract(config.getTotalOutput())).compareTo(new BigDecimal(1000)) > 0) {
                config.setTotalOutput(config.getTotalOutput().add(BigDecimal.valueOf(Math.random() * 100)));
            } else {
                config.setTotalOutput(config.getTotalOutput().subtract(BigDecimal.valueOf(Math.random() * 100)));
            }
            if ((redisConfig.getNotReward().subtract(config.getNotReward())).compareTo(new BigDecimal(300)) > 0) {
                config.setNotReward(config.getNotReward().add(BigDecimal.valueOf(Math.random() * 20)));
            } else {
                config.setNotReward(config.getNotReward().subtract(BigDecimal.valueOf(Math.random() * 20)));
            }
            if ((redisConfig.getWaitBuyBack().subtract(config.getWaitBuyBack())).compareTo(new BigDecimal(300)) > 0) {
                config.setWaitBuyBack(config.getWaitBuyBack().add(BigDecimal.valueOf(Math.random() * 20)));
            } else {
                config.setWaitBuyBack(config.getWaitBuyBack().subtract(BigDecimal.valueOf(Math.random() * 20)));
            }
            if ((redisConfig.getBuyBackDestruction().subtract(config.getBuyBackDestruction())).compareTo(new BigDecimal(300)) > 0) {
                config.setBuyBackDestruction(config.getBuyBackDestruction().add(BigDecimal.valueOf(Math.random() * 20)));
            } else {
                config.setBuyBackDestruction(config.getBuyBackDestruction().subtract(BigDecimal.valueOf(Math.random() * 20)));
            }
            if (redisConfig.getValidNode() - config.getValidNode() > 50) {
                config.setValidNode((long) (config.getValidNode() + (Math.random() * 3)));
            } else {
                config.setValidNode((long) (config.getValidNode() - (Math.random() * 3)));
            }
            if (redisConfig.getPartakeCount() - config.getPartakeCount() > 50) {
                config.setPartakeCount((long) (config.getPartakeCount() + (Math.random() * 3)));
            } else {
                config.setPartakeCount((long) (config.getPartakeCount() - (Math.random() * 3)));
            }
        }
        configMapper.updateMiningConfig(config);
    }
}
